package de.freudenreichdesign.webshop.controller;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import de.freudenreichdesign.webshop.model.Article;
import de.freudenreichdesign.webshop.service.ArticleService;

@Controller
@RequestMapping("/admin/createNewArticle")
public class AdminCreateNewProductDetailController{

	private static final String UPLOAD_FOLDER_PATH = "E:/Webprogrammierung Projekt/Workspace/de.freudenreichdesign.webshop/WebContent";
	
	@RequestMapping(method=RequestMethod.GET)
	public String showFormNewArticle(ModelMap modelMap) {
		Article article = new Article();
		modelMap.addAttribute(article);
		return "adminProductDetailView";
	}
	
	@RequestMapping(method=RequestMethod.POST)
    public String handleFormInputAndUpload(HttpServletRequest request, @ModelAttribute("Article") Article article,
        @RequestParam("fileSmallImage") MultipartFile fileSmallImage, @RequestParam("fileLargeImage") MultipartFile fileLargeImage) throws IOException {
		//try {
			InputStream inputStream = null;
			OutputStream outputStream = null;
			String fileSmallImageName = null;
			String fileLargeImageName = null;
			if (!fileSmallImage.isEmpty()) {
				if (fileSmallImage.getSize() > 0) {
        			inputStream = fileSmallImage.getInputStream();
        			if (fileSmallImage.getSize() > 1000000)
        				return "/";
        			fileSmallImageName = "/images/" + fileSmallImage.getOriginalFilename();
        			outputStream = new FileOutputStream(UPLOAD_FOLDER_PATH + fileSmallImageName);
        			int readBytes = 0;
        			byte[] buffer = new byte[1000000];
        			while ((readBytes = inputStream.read(buffer, 0, 1000000)) != -1)
        				outputStream.write(buffer, 0, readBytes);
				}
			}
			if (!fileLargeImage.isEmpty()) {
				if (fileLargeImage.getSize() > 0) {
	        		inputStream = fileLargeImage.getInputStream();
	        		if (fileLargeImage.getSize() > 3000000)
	        			return "/";
	        		fileLargeImageName = "/images/" + fileLargeImage.getOriginalFilename();
	        		outputStream = new FileOutputStream(UPLOAD_FOLDER_PATH + fileLargeImageName);
	        		int readBytes = 0;
	        		byte[] buffer = new byte[3000000];
	        		while ((readBytes = inputStream.read(buffer, 0, 3000000)) != -1)
	        			outputStream.write(buffer, 0, readBytes);
				}
			}
    		outputStream.close();
    		inputStream.close();
    		ArticleService.getInstance().createArticle(article.getTitle(),
				article.getHeight(),
				article.getDescription(),
				article.getLength(),
				article.getDepth(),
				article.getWeight(),
				article.getPrice(),
				article.getTax(),
				article.getActiveSince(),
				fileSmallImageName,
				fileLargeImageName);
			return "redirect:/admin/adminProductGallery.html";
		//} catch (Exception e) {}
		//return "redirect:uploadFailure";
	}
	
	@InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false));
    }

}
